<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/view/common/ztreecommon.jsp"%>
<!DOCTYPE html>
<html>
	<head>
		<title></title>
		<style type="text/css">
	    html, body{
	        margin:0;padding:0;border:0;width:100%;height:100%;
	    }    
	    </style>
	<SCRIPT type="text/javascript">
		function saveRoleMenu(){
			var roletree = $.fn.zTree.getZTreeObj("roletree"),
			roletreenode = roletree.getSelectedNodes();
			if(roletreenode==null||roletreenode==''){
				return toastr.info("请先选择岗位", "提示", toastrOptsBottom);
			}
			var menutree = $.fn.zTree.getZTreeObj("menutree");
			menutree = menutree.getCheckedNodes();
			if(menutree.length==0){
				return toastr.info("请至少选择一个菜单再进行保存", "提示", toastrOptsBottom);
			}
			var menus = "";
			for(var i=0;i<menutree.length;i++){
				menus += menutree[i].id+",";
			}
			$.ajax({
		        url: "${path}/assist/saveRoleMenu.action",
		        data: {roleid:roletreenode[0].id,menuids:menus},
		        success: function (text) {
		        	toastr.info(text.msg, "提示", toastrOptsBottom);
		        },
		        error: function (jqXHR, textStatus, errorThrown) {
		        	toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
		        }
		    });
        };
        
        var is_select = true;
        function selectAll(){
        	var menutree = $.fn.zTree.getZTreeObj("menutree");
        	if(is_select){
        		menutree.checkAllNodes(true);
        		is_select = false;
        	}else{
        		menutree.checkAllNodes(false);
        		is_select = true;
        	}
        }
		
		
		$(document).ready(function(){
		
			var setting = {
				data: {
					key: {
						title:"notes"
					},
					simpleData: {
						enable: true
					}
				},
				callback: {
					beforeClick: beforeClick
				}
			};
			
			var settingmenu = {
				check: {
					enable: true
				},
				data: {
					simpleData: {
						enable: true
					}
				}
			};
			
			function beforeClick(treeId, treeNode) {
				var isleaf = treeNode.isleaf;
				if (treeNode.id.length == 11) {
					return true;
				} else {
					$.ajax({
				        url: "${path}/assist/menuZtree",
				        data: {user_priv_id:treeNode.id},
				        success: function (text) {
							$.fn.zTree.init($("#menutree"), settingmenu, text);
				        },
				        error: function (jqXHR, textStatus, errorThrown) {
				        	toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
				        }
				    });
					//document.mainframe.location = treeNode.value;
					return true;
				}
			};
		
		    $.ajax({
		        url: "${path}/assist/menuZtree",
		        success: function (text) {
					$.fn.zTree.init($("#menutree"), settingmenu, text);
		        },
		        error: function (jqXHR, textStatus, errorThrown) {
		        	toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
		        }
		    });
			
		    $.ajax({
		        url: "${path}/assist/roleList",
		        data: {},
		        success: function (text) {
					$.fn.zTree.init($("#roletree"), setting, text);
		        },
		        error: function (jqXHR, textStatus, errorThrown) {
		        	toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
		        }
		    });
		    
		    $('#roletreediv').css("height",document.body.clientHeight-38);
		    $('#menutreediv').css("height",document.body.clientHeight-38);
		})
	    
	</SCRIPT>
	</head>
	<style>
		.ztoolbar{
			background-color:#EEEEEE;
			padding-left:10px
		}
		body{
		}
	</style>
	<body>
		<div class="ztoolbar">
			<input class="btn btn-default" type="button" value="保存" onclick="saveRoleMenu()">
			<input class="btn btn-default" type="button" value="菜单全选" onclick="selectAll()">
		</div>
			
	    <div title="部门岗位" id="roletreediv"  style="float:left;width:40%;padding:10px;overflow-y:auto">
	    	<ul id="roletree" class="ztree"></ul>
	    </div>
	    <div title="菜单" id="menutreediv" style="float:left;padding:10px;width:60%;overflow-y:auto">
	    	<ul id="menutree" class="ztree"></ul>
	    </div>
	</body>
</html>